<template>
  <div class="app-container">
    <el-form ref="student" :model="student" label-width="120px">
      <el-row type="flex">
        <el-col :span="12">
          <el-form-item label="学号" prop="sno">
            <el-input v-model="student.sno" maxlength="10"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="student.name"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="12">
          <el-form-item label="性别" prop="gender">
            <el-select v-model="student.gender" placeholder="请选择">
              <el-option
                v-for="gender in genders"
                :key="gender.value"
                :label="gender.label"
                :value="gender.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="民族" prop="nationality">
            <el-input v-model="student.nationality"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="12">
          <el-form-item label="政治面貌" prop="politicStatus">
            <el-select v-model="student.politicStatus" placeholder="请选择">
              <el-option
                v-for="ps in politicStatus"
                :key="ps.value"
                :label="ps.label"
                :value="ps.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出生日期" prop="birthday">
            <el-date-picker
              v-model="student.birthday"
              type="date"
              placeholder="选择日期"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="12">
          <el-form-item label="身份证号" prop="idno">
            <el-input v-model="student.idno"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="家庭住址" prop="address">
            <el-input v-model="student.address"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="12">
          <el-form-item label="年级" prop="gradeId">
            <el-select v-model="student.gradeId" filterable placeholder="请选择">
              <el-option
                v-for="grade in grades"
                :key="grade.id"
                :label="grade.gradeName"
                :value="grade.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="专业" prop="majorId">
            <el-select v-model="student.majorId" filterable placeholder="请选择" @change="majorChanged">
              <el-option
                v-for="major in majors"
                :key="major.id"
                :label="major.majorName"
                :value="major.id"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="12">
          <el-form-item label="班级" prop="classId">
            <el-select v-model="student.classId" filterable placeholder="请选择">
              <el-option
                v-for="item in classs"
                :key="item.id"
                :label="item.className"
                :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="student.phone"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="12">
          <el-form-item label="入学日期" prop="enrollDate">
            <el-date-picker
              v-model="student.enrollDate"
              type="date"
              placeholder="选择日期"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="毕业日期" prop="graduateDate">
            <el-date-picker
              v-model="student.graduateDate"
              type="date"
              placeholder="选择日期"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="student.remark"
          type="textarea"
          placeholder="请输入内容"
          maxlength="30"
          show-word-limit/>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
        <el-button @click="resetForm('student')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="scss" scoped>
  .app-container{
    width: 800px;
  }
</style>

<script>
import student from '@/api/student/student'
import classs from '@/api/student/classs'
import major from '@/api/student/major'
import grade from '@/api/student/grade'

const defaultForm = {
  sno: '',
  name: '',
  gender: '',
  nationality: '',
  politicStatus: '',
  birthday: '',
  idno: '',
  address: '',
  classId: '',
  gradeId: '',
  majorId: '',
  phone: '',
  enrollDate: '',
  graduateDate: '',
  remark: ''
}
// 时间格式化
const moment = require('moment')

export default {

  filters: {

  },
  data() {
    return {
      // 性别
      genders: [{
        value: 1,
        label: '男'
      }, {
        value: 0,
        label: '女'
      }],
      // 政治面貌
      politicStatus: [
        {
          value: 'TY',
          label: '团员'
        },
        {
          value: 'DY',
          label: '党员'
        },
        {
          value: 'QZ',
          label: '群众'
        },
        {
          value: 'QT',
          label: '其他'
        }
      ],
      student: defaultForm,
      majors: [], // 专业
      grades: [], // 年级
      classs: [], // 班级
      saveBtnDisabled: false, // 保存按钮是否禁用,
      BASE_API: process.env.BASE_API // 接口API地址
    }
  },

  watch: {
    $route(to, from) {
      this.init()
    }
  },

  created() {
    this.init()
  },

  methods: {
    init() {
      if (this.$route.params && this.$route.params.id) { // 更新
        const id = this.$route.params.id
        this.fetchDataById(id)
      } else { // 新增
        // 使用对象拓展运算符，拷贝对象，而不是引用
        this.student = { ...defaultForm }
      }

      // 获取专业列表
      major.getList().then(response => {
        // debugger 设置断点调试
        if (response.success === true) {
          this.majors = response.data.items
        }
      })

      // 获取年级列表
      grade.getList().then(response => {
        // debugger 设置断点调试
        if (response.success === true) {
          this.grades = response.data.items
        }
      })

      // // 根据年级获取班级列表
      // classs.getList(this.grades).then(response => {
      //   // debugger 设置断点调试
      //   if (response.success === true) {
      //     this.classs1 = response.data.items
      //   }
      // })
    },
    // 修改专业触发
    majorChanged(value) {
      // 先清空之前的选项
      this.student.classId = ''
      // 获取专业列表
      classs.getList(value).then(response => {
        // debugger 设置断点调试
        if (response.success === true) {
          this.classs = response.data.items
        }
      })
    },
    // 保存
    saveOrUpdate() {
      this.saveBtnDisabled = true
      if (this.student.birthday === '' || this.student.birthday == null || this.student.birthday === undefined) {
        this.student.birthday = null
      } else {
        this.student.birthday = moment(this.student.birthday).format('YYYY-MM-DD')
      }
      if (this.student.enrollDate === '' || this.student.enrollDate == null || this.student.enrollDate === undefined) {
        this.student.enrollDate = null
      } else {
        this.student.enrollDate = moment(this.student.enrollDate).format('YYYY-MM-DD')
      }
      if (this.student.graduateDate === '' || this.student.graduateDate == null || this.student.graduateDate === undefined) {
        this.student.graduateDate = null
      } else {
        this.student.graduateDate = moment(this.student.graduateDate).format('YYYY-MM-DD')
      }

      console.log(JSON.stringify(this.student))
      student.save(this.student).then(response => {
        console.log(response)
        return this.$message({
          type: 'success',
          message: response.message
        })
      }).then(resposne => {
        this.$router.push({ path: '/student' })
      })
    },

    // 根据id查询记录
    fetchDataById(id) {
      student.getById(id).then(response => {
        this.student = response.data.item
      })
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }

  }
}
</script>
